﻿<cfcomponent >
	
	
	<cffunction 
		name="readsettings" 
		output="true"
		returntype="void"
	>
	
	<cftry>
	<cfquery name="qryEmailTemplate" datasource="IBOSE_GLOBAL" >
		SELECT NAME, BODYTPL, SUBJECTTPL
	      FROM EGLKEMAILTEMPLATE;
	</cfquery>
	
	<cfif isdefined("qryEmailTemplate") >
		<cfoutput>
		{
		"success": "true",
		"data": [
				<cfset ecounter = 1 >
				<cfset querylen = qryEmailTemplate.recordcount >
				<cfloop query="qryEmailTemplate">
					{
						"NAME": #SerializeJSON(NAME)#, 
						"BODYTEMPLATE": #SerializeJSON(BODYTPL)#,
						"SUBJECTTEMPLATE": #SerializeJSON(SUBJECTTPL)#
					}
					<cfif querylen NEQ ecounter> 
						<cfoutput>,</cfoutput>
				    </cfif>
				    <cfset ecounter = ecounter + 1 >
				    
		        </cfloop>
		]
		}
		</cfoutput>
	<cfelse>
		<cfoutput>
		{
		"success": "true",
		"data": [{
						"NAME": #SerializeJSON("")#, 
						"BODYTEMPLATE": #SerializeJSON("")#,
						"SUBJECTTEMPLATE": #SerializeJSON("")#
					}]
		}
		</cfoutput>
	</cfif>
		<cfcatch>
			<cfoutput>
				"success": "true",
				"data": [{
						"NAME": #SerializeJSON(cfcatch.Detail)#, 
						"BODYTEMPLATE": #SerializeJSON(cfcatch.Message)#,
						"SUBJECTTEMPLATE": #SerializeJSON("")#
					}]
				}
			</cfoutput>
		</cfcatch>
	</cftry>
	</cffunction>
	
	
	<cffunction 
		name="createsettings" 
		output="true"
		returntype="void"
	>
	
	<cftry>
		
	<!---<cfif (cgi.content_type EQ "application/json") >
	</cfif>--->  
	
    <cfset myData      = deserializeJSON(ToString(getHTTPRequestData().content)) >
    <cfset thename     = myData.data.NAME >
	<cfset thebody     = myData.data.BODYTEMPLATE >
	<cfset thetemplate = myData.data.SUBJECTTEMPLATE >
	
	<cfset ans = findnocase("<script>", thebody) >
	<cfif ans NEQ 0 >
		<cfset thebody = left(thebody, ans - 1) >
	</cfif>
	<cfset anss = findnocase("<script>", thetemplate) >
	<cfif anss NEQ 0 >
		<cfset thetemplate = left(thetemplate, anss - 1) >
	</cfif>
	
	<cfquery name="qryEmailTemplate" datasource="IBOSE_GLOBAL" >
		SELECT NAME, BODYTPL, SUBJECTTPL
	      FROM EGLKEMAILTEMPLATE
	     WHERE NAME = '#thename#';
	</cfquery>
	
	<cfif isdefined("qryEmailTemplate") >
		<cfif qryEmailTemplate.recordcount GT 0 >
			<cfthrow detail="Record already exist"  >
		<cfelse>
			<cfquery name="insertEmailTemplate" datasource="IBOSE_GLOBAL" >
				INSERT INTO EGLKEMAILTEMPLATE (EMAILTEMPLATEID,
											   NAME, 
											   BODYTPL, 
											   SUBJECTTPL)
			         VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#createuuid()#" >,
			                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#thename#" >,
			                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#thebody#" >,
			                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#thetemplate#" >);
			</cfquery>
			
		</cfif>
	
	<cfelse>
		<cfquery name="insertEmailTemplate" datasource="IBOSE_GLOBAL" >
			INSERT INTO EGLKEMAILTEMPLATE (EMAILTEMPLATEID,
										   NAME, 
										   BODYTPL, 
										   SUBJECTTPL)
		         VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#createuuid()#" >,
		                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#thename#" >,
		                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#thebody#" >,
		                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#thetemplate#" >);
		</cfquery>
		
	</cfif>
	
	
	<cfif isdefined("qryEmailTemplate") >
		<cfoutput>
		{
		"success": "true",
		"message": "Successful!",
		"data": [
				<cfset ecounter = 1 >
				<cfset querylen = qryEmailTemplate.recordcount >
				<cfloop query="qryEmailTemplate">
					{
						"NAME": #SerializeJSON(NAME)#, 
						"BODYTEMPLATE": #SerializeJSON(BODYTPL)#,
						"SUBJECTTEMPLATE": #SerializeJSON(SUBJECTTPL)#
					}
					<cfif querylen NEQ ecounter> 
						<cfoutput>,</cfoutput>
				    </cfif>
				    <cfset ecounter = ecounter + 1 >
				    
		        </cfloop>
		]
		}
		</cfoutput>
	<cfelse>
		<cfoutput>
		{
		"success": "true",
		"message": "Empty record!",
		"data": [{
						"NAME": #SerializeJSON("")#, 
						"BODYTEMPLATE": #SerializeJSON("")#,
						"SUBJECTTEMPLATE": #SerializeJSON("")#
					}]
		}
		</cfoutput>
	</cfif>
		<cfcatch>
			<cfoutput>
				{
				"success": "true",
				"message": #SerializeJSON('Failed! ' & cfcatch.Detail)#,
				"data": [{
						"NAME": #SerializeJSON(cfcatch.Detail)#, 
						"BODYTEMPLATE": #SerializeJSON(cfcatch.Message)#,
						"SUBJECTTEMPLATE": ""
					}]
				}
			</cfoutput>
		</cfcatch>
	</cftry>
	</cffunction>	


	<cffunction 
		name="updatesettings" 
		output="true"
		returntype="void"
	>
	
	<cftry>
	
	
    <cfset myData      = deserializeJSON(ToString(getHTTPRequestData().content)) >
    <cfset thename     = myData.data.NAME >
	<cfset thebody     = myData.data.BODYTEMPLATE >
	<cfset thetemplate = myData.data.SUBJECTTEMPLATE >
	
	<cfset ans = findnocase("<script>", thebody) >
	<cfif ans NEQ 0 >
		<cfset thebody = left(thebody, ans - 1) >
	</cfif>
	<cfset anss = findnocase("<script>", thetemplate) >
	<cfif anss NEQ 0 >
		<cfset thetemplate = left(thetemplate, anss - 1) >
	</cfif>
	
	<cfquery name="qryEmailTemplate" datasource="IBOSE_GLOBAL" >
		UPDATE EGLKEMAILTEMPLATE
		   SET BODYTPL = <cfqueryparam cfsqltype="cf_sql_varchar" value="#thebody#" >,
		       SUBJECTTPL = <cfqueryparam cfsqltype="cf_sql_varchar" value="#thetemplate#" >
		 WHERE NAME = '#thename#';
	</cfquery>
	
	
	<cfoutput>
		{
		"success": "true",
		"message": "Successful",
		"data": [{
				"NAME": #SerializeJSON(thename)#, 
				"BODYTEMPLATE": #SerializeJSON(thebody)#, 
				"SUBJECTTEMPLATE": #SerializeJSON(thetemplate)#, 
				}]
		}
		</cfoutput>
	
		<cfcatch>
			<cfoutput>
				"success": "true",
				"message": "Failed!",
				"data": [{
						"NAME": #SerializeJSON(cfcatch.Detail)#, 
						"BODYTEMPLATE": #SerializeJSON(cfcatch.Message)#,
						"SUBJECTTEMPLATE": #SerializeJSON("")#
					}]
				}
			</cfoutput>
		</cfcatch>
	</cftry>
	</cffunction>	
	
	<cffunction 
		name="destroysettings" 
		output="true"
		returntype="void"
	>
	
	<cftry>
	
	
    <cfset myData      = deserializeJSON(ToString(getHTTPRequestData().content)) >
    <cfset thename     = myData.data.NAME >
	<cfset thebody     = myData.data.BODYTEMPLATE >
	<cfset thetemplate = myData.data.SUBJECTTEMPLATE >
	
	<cfset ans = findnocase("<script>", thebody) >
	<cfif ans NEQ 0 >
		<cfset thebody = left(thebody, ans - 1) >
	</cfif>
	<cfset anss = findnocase("<script>", thetemplate) >
	<cfif anss NEQ 0 >
		<cfset thetemplate = left(thetemplate, anss - 1) >
	</cfif>
	
	<cfquery name="qryEmailTemplate" datasource="IBOSE_GLOBAL" >
		DELETE FROM EGLKEMAILTEMPLATE
		 WHERE NAME = '#thename#';
	</cfquery>
	
	
	<cfoutput>
		{
		"success": "true",
		"message": "Successful!",
		"data": [{
				"NAME": #SerializeJSON(thename)#, 
				"BODYTEMPLATE": #SerializeJSON(thebody)#, 
				"SUBJECTTEMPLATE": #SerializeJSON(thetemplate)#, 
				}]
		}
		</cfoutput>
	
		<cfcatch>
			<cfoutput>
				"success": "true",
				"message": "Failed!",
				"data": [{
						"NAME": #SerializeJSON(cfcatch.Detail)#, 
						"BODYTEMPLATE": #SerializeJSON(cfcatch.Message)#,
						"SUBJECTTEMPLATE": #SerializeJSON("")#
					}]
				}
			</cfoutput>
		</cfcatch>
	</cftry>
	</cffunction>	
	
	
	<!---Clearing Department--->
		
	<cffunction 
		name="clrreadsettings" 
		output="true"
		returntype="void"
	>
	
	<cftry>
	<cfquery name="qryEmailTemplate" datasource="IBOSE_GLOBAL" >
		SELECT CLEARINGDEPT, THEGROUP, OWNER, EMAILADDRESS, DATAINDEXSUFFIX
	      FROM EGLKCLRDEPT;
	</cfquery>
	
	<cfif isdefined("qryEmailTemplate") >
		<cfoutput>
		{
		"success": "true",
		"data": [
				<cfset ecounter = 1 >
				<cfset querylen = qryEmailTemplate.recordcount >
				<cfloop query="qryEmailTemplate">
					{
						"CLEARINGDEPT": #SerializeJSON(CLEARINGDEPT)#, 
						"THEGROUP": #SerializeJSON(THEGROUP)#,
						"OWNER": #SerializeJSON(OWNER)#,
						"EMAILADDRESS": #SerializeJSON(EMAILADDRESS)#,
						"DATAINDEXSUFFIX": #SerializeJSON(DATAINDEXSUFFIX)#
					}
					<cfif querylen NEQ ecounter> 
						<cfoutput>,</cfoutput>
				    </cfif>
				    <cfset ecounter = ecounter + 1 >
				    
		        </cfloop>
		]
		}
		</cfoutput>
	<cfelse>
		<cfoutput>
		{
		"success": "true",
		"data": [{
						"CLEARINGDEPT": #SerializeJSON("")#, 
						"THEGROUP": #SerializeJSON("")#,
						"OWNER": #SerializeJSON("")#,
						"EMAILADDRESS": #SerializeJSON("")#,
						"DATAINDEXSUFFIX": #SerializeJSON("")#
					}]
		}
		</cfoutput>
	</cfif>
		<cfcatch>
			<cfoutput>
				"success": "true",
				"data": [{
						"CLEARINGDEPT": #SerializeJSON(cfcatch.detail)#, 
						"THEGROUP": #SerializeJSON(cfcatch.message)#,
						"OWNER": #SerializeJSON("")#,
						"EMAILADDRESS": #SerializeJSON("")#,
						"DATAINDEXSUFFIX": #SerializeJSON("")#
					}]
				}
			</cfoutput>
		</cfcatch>
	</cftry>
	</cffunction>
	
	
	<cffunction 
		name="clrcreatesettings" 
		output="true"
		returntype="void"
	>
	
	<cftry>
		

	
    <cfset myData      = deserializeJSON(ToString(getHTTPRequestData().content)) >
    <cfset CLEARINGDEPT     = myData.data.CLEARINGDEPT >
	<cfset THEGROUP     = myData.data.THEGROUP >
	<cfset OWNER = myData.data.OWNER >
	<cfset EMAILADDRESS = myData.data.EMAILADDRESS >
	<cfset DATAINDEXSUFFIX = myData.data.DATAINDEXSUFFIX >
	
	<cfquery name="qryEmailTemplate" datasource="IBOSE_GLOBAL" >
		SELECT CLEARINGDEPT, THEGROUP, OWNER, EMAILADDRESS, DATAINDEXSUFFIX
	      FROM EGLKCLRDEPT
	     WHERE CLEARINGDEPT = '#CLEARINGDEPT#' AND EMAILADDRESS = '#EMAILADDRESS#' AND THEGROUP = '#THEGROUP#';
	</cfquery>
	
	<cfif isdefined("qryEmailTemplate") >
		<cfif qryEmailTemplate.recordcount GT 0 >
			<cfthrow detail="Record already exist"  >
		<cfelse>
			<cfquery name="insertEmailTemplate" datasource="IBOSE_GLOBAL" >
				INSERT INTO EGLKCLRDEPT (  ID,
										   CLEARINGDEPT,
										   THEGROUP, 
										   OWNER, 
										   EMAILADDRESS,
										   DATAINDEXSUFFIX)
			         VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#createuuid()#" >,
			                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#CLEARINGDEPT#" >,
							 <cfqueryparam cfsqltype="cf_sql_varchar" value="#THEGROUP#" >,
							 <cfqueryparam cfsqltype="cf_sql_varchar" value="#OWNER#" >,
			                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#EMAILADDRESS#" >,
			                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#DATAINDEXSUFFIX#" >);
			</cfquery>
			
		</cfif>
	
	<cfelse>
		<cfquery name="insertEmailTemplate" datasource="IBOSE_GLOBAL" >
			INSERT INTO EGLKCLRDEPT (  ID,
										   CLEARINGDEPT,
										   THEGROUP, 
										   OWNER, 
										   EMAILADDRESS,
										   DATAINDEXSUFFIX)
			         VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#createuuid()#" >,
			                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#CLEARINGDEPT#" >,
							 <cfqueryparam cfsqltype="cf_sql_varchar" value="#THEGROUP#" >,
							 <cfqueryparam cfsqltype="cf_sql_varchar" value="#OWNER#" >,
			                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#EMAILADDRESS#" >,
			                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#DATAINDEXSUFFIX#" >);
		</cfquery>
		
	</cfif>
	
	
	<cfif isdefined("qryEmailTemplate") >
		<cfoutput>
		{
		"success": "true",
		"message": "Successful!",
		"data": [
				<cfset ecounter = 1 >
				<cfset querylen = qryEmailTemplate.recordcount >
				<cfloop query="qryEmailTemplate">
					{
						"CLEARINGDEPT": #SerializeJSON(CLEARINGDEPT)#, 
						"THEGROUP": #SerializeJSON(THEGROUP)#,
						"OWNER": #SerializeJSON(OWNER)#,
						"EMAILADDRESS": #SerializeJSON(EMAILADDRESS)#,
						"DATAINDEXSUFFIX": #SerializeJSON(DATAINDEXSUFFIX)#
					}
					<cfif querylen NEQ ecounter> 
						<cfoutput>,</cfoutput>
				    </cfif>
				    <cfset ecounter = ecounter + 1 >
				    
		        </cfloop>
		]
		}
		</cfoutput>
	<cfelse>
		<cfoutput>
		{
		"success": "true",
		"message": "Empty record!",
		"data": [{
						"CLEARINGDEPT": #SerializeJSON("")#, 
						"THEGROUP": #SerializeJSON("")#,
						"OWNER": #SerializeJSON("")#,
						"EMAILADDRESS": #SerializeJSON("")#,
						"DATAINDEXSUFFIX": #SerializeJSON("")#
					}]
		}
		</cfoutput>
	</cfif>
		<cfcatch>
			<cfoutput>
				{
				"success": "true",
				"message": #SerializeJSON('Failed! ' & cfcatch.Detail)#,  
				"data": [{
						"CLEARINGDEPT": #SerializeJSON(cfcatch.detail)#, 
						"THEGROUP": #SerializeJSON(cfcatch.message)#,
						"OWNER": #SerializeJSON("")#,
						"EMAILADDRESS": #SerializeJSON("")#,
						"DATAINDEXSUFFIX": #SerializeJSON("")#
					}]
				}
			</cfoutput>
		</cfcatch>
	</cftry>
	</cffunction>	



	<cffunction 
		name="clrupdatesettings" 
		output="true"
		returntype="void"
	>
	
	<cftry>
	
	<cfset myData      = deserializeJSON(ToString(getHTTPRequestData().content)) >
    <cfset CLEARINGDEPT     = myData.data.CLEARINGDEPT >
	<cfset THEGROUP     = myData.data.THEGROUP >
	<cfset OWNER = myData.data.OWNER >
	<cfset EMAILADDRESS = myData.data.EMAILADDRESS >
	<cfset DATAINDEXSUFFIX = myData.data.DATAINDEXSUFFIX >
	
	
	
	<cfquery name="qryEmailTemplate" datasource="IBOSE_GLOBAL" >
		UPDATE EGLKCLRDEPT
		   SET THEGROUP = <cfqueryparam cfsqltype="cf_sql_varchar" value="#THEGROUP#" >,
		       OWNER = <cfqueryparam cfsqltype="cf_sql_varchar" value="#OWNER#" >,
		       DATAINDEXSUFFIX = <cfqueryparam cfsqltype="cf_sql_varchar" value="#DATAINDEXSUFFIX#" >
		 WHERE CLEARINGDEPT = '#CLEARINGDEPT#' AND EMAILADDRESS = '#EMAILADDRESS#' AND THEGROUP = '#THEGROUP#';
	</cfquery>
	
	
	<cfoutput>
		{
		"success": "true",
		"message": "Successful",
		"data": [{
				        "CLEARINGDEPT": #SerializeJSON(CLEARINGDEPT)#, 
						"THEGROUP": #SerializeJSON(THEGROUP)#,
						"OWNER": #SerializeJSON(OWNER)#,
						"EMAILADDRESS": #SerializeJSON(EMAILADDRESS)#,
						"DATAINDEXSUFFIX": #SerializeJSON(DATAINDEXSUFFIX)#
				}]
		}
		</cfoutput>
	
		<cfcatch>
			<cfoutput>
				"success": "true",
				"message": "Failed!",
				"data": [{
						"CLEARINGDEPT": #SerializeJSON(cfcatch.detail)#, 
						"THEGROUP": #SerializeJSON(cfcatch.message)#,
						"OWNER": #SerializeJSON("")#,
						"EMAILADDRESS": #SerializeJSON("")#,
						"DATAINDEXSUFFIX": #SerializeJSON("")#
					}]
				}
			</cfoutput>
		</cfcatch>
	</cftry>
	</cffunction>	
	
	<cffunction 
		name="clrdestroysettings" 
		output="true"
		returntype="void"
	>
	
	<cftry>
	
	<cfset myData      = deserializeJSON(ToString(getHTTPRequestData().content)) >
    <cfset CLEARINGDEPT     = myData.data.CLEARINGDEPT >
	<cfset THEGROUP     = myData.data.THEGROUP >
	<cfset OWNER = myData.data.OWNER >
	<cfset EMAILADDRESS = myData.data.EMAILADDRESS >
	<cfset DATAINDEXSUFFIX = myData.data.DATAINDEXSUFFIX >
	
	
	<cfquery name="qryEmailTemplate" datasource="IBOSE_GLOBAL" >
		DELETE FROM EGLKCLRDEPT
		 WHERE CLEARINGDEPT = '#CLEARINGDEPT#' AND EMAILADDRESS = '#EMAILADDRESS#' AND THEGROUP = '#THEGROUP#';
	</cfquery>
	
	
	<cfoutput>
		{
		"success": "true",
		"message": "Successful!",
		"data": [{
				        "CLEARINGDEPT": #SerializeJSON(CLEARINGDEPT)#, 
						"THEGROUP": #SerializeJSON(THEGROUP)#,
						"OWNER": #SerializeJSON(OWNER)#,
						"EMAILADDRESS": #SerializeJSON(EMAILADDRESS)#,
						"DATAINDEXSUFFIX": #SerializeJSON(DATAINDEXSUFFIX)#
				}]
		}
		</cfoutput>
	
		<cfcatch>
			<cfoutput>
				"success": "true",
				"message": "Failed!",
				"data": [{
						"CLEARINGDEPT": #SerializeJSON(cfcatch.detail)#, 
						"THEGROUP": #SerializeJSON(cfcatch.message)#,
						"OWNER": #SerializeJSON("")#,
						"EMAILADDRESS": #SerializeJSON("")#,
						"DATAINDEXSUFFIX": #SerializeJSON("")#
					}]
				}
			</cfoutput>
		</cfcatch>
	</cftry>
	</cffunction>
	
	
	
	

</cfcomponent>